using System;

namespace Winsmarts.SSPC.Common
{
    public class ProgramInputs
    {
        private string sspURL = "";
        private bool isValid = false;
        private ProgramOperation currentOperation;
        private string fileName = "";
        private bool fullCrawlOption = false;
        private bool startCompilation = false;
        private bool removeExcessMappings = false;
        private OperationTarget currentOperationTarget;

        public string FileName
        {
            get { return fileName; }
        }

        public string SSPURL
        {
            get { return sspURL; }
        }

        public bool IsValid
        {
            get { return isValid; }
        }

        public ProgramOperation CurrentOperation
        {
            get { return currentOperation; }
        }

        public bool FullcrawlOption
        {
            get { return fullCrawlOption; }
        }

        public OperationTarget CurrentOperationTarget
        {
            get { return currentOperationTarget; }
        }

        public bool StartCompilation
        {
            get { return startCompilation; }
        }

        public bool RemoveExcessMappings
        {
            get { return removeExcessMappings; }
        }

        public ProgramInputs(string[] args)
        {
            if (args.Length == 0)
            {
                Trace.PrintHelp();
                return;
            }

            // What are we going to do?
            switch (args[0].ToLower())
            {
                case "-help":
                    Trace.PrintHelp();
                    break;
                case "-o":
                    if (args.Length >= 3)
                    {
                        SetCurrentOperation(args[1], args[2]);
                    }
                    break;
                default:
                    break;
            }

            if (currentOperation == ProgramOperation.Invalid)
            {
                Trace.WriteLine("Invalid Operation specified", true);
                return;
            }

            // What are the parameters?
            for (int indexer = 3; indexer < args.Length; indexer++)
            {
                //don't care about casing
                switch (args[indexer].ToLower())
                {
                    case "-url":
                        sspURL = args[++indexer];
                        break;
                    
                    case "-file":
                        fileName = args[++indexer];
                        break;
                    
                    case "-fullcrawl":
                        fullCrawlOption = true;
                        break;

                    case "-startcompilation":
                        startCompilation = true;
                        break;
                        
                    case "-removeexcessmappings":
                        removeExcessMappings = true;
                        break;

                    default:
                        Trace.WriteLine("Invalid parameter specified will be ignored: " + args[indexer], true);
                        Trace.PrintHelp();
                        break;
                }
            }

            //So far so good
            if (
                fileName.Length != 0 &&
                sspURL.Length != 0 &&
                currentOperation != ProgramOperation.Invalid &&
                currentOperationTarget != OperationTarget.Invalid &&
                (!startCompilation || currentOperationTarget == OperationTarget.SearchScopes) &&
                (!fullCrawlOption || currentOperationTarget == OperationTarget.ContentSources) &&
                (!removeExcessMappings || currentOperationTarget == OperationTarget.ManagedProperties))
            {
                isValid = true;
            }
        }

        private void SetCurrentOperation(string requestedOperation, string operationType)
        {
            try
            {
                currentOperation = (ProgramOperation) Enum.Parse(typeof (ProgramOperation), requestedOperation, true);
                currentOperationTarget = (OperationTarget) Enum.Parse(typeof (OperationTarget), operationType, true);
            }
            catch( ArgumentException )
            {
                currentOperation = ProgramOperation.Invalid;
                currentOperationTarget = OperationTarget.Invalid;
            }
        }
    }
}